# ### 操作类
import os
import pickle
import random
from .card import Card
from .person import Person


class Operation():
    def __init__(self):
        # 加载数据 卡号: 用户对象
        self.load_user()
        self.load_userid()

    def load_user(self):
        if os.path.exists("user.txt"):
            with open("user.txt", mode="rb") as fp:
                self.user_dict = pickle.load(fp)
        else:
            self.user_dict = {}
        print(self.user_dict)

    def load_userid(self):
        if os.path.exists("userid.txt"):
            with open("userid.txt", mode="rb") as fp:
                self.userid_dict = pickle.load(fp)
        else:
            pass
        print(self.userid_dict)

    def save(self):
        with open("user.txt", "wb") as fp:
            pickle.dump(self.user_dict, fp)

        with open("userid.txt", "wb") as fp:
            pickle.dump(self.userid_dict, fp)

    def register(self):
        name = input("请输入您的姓名:")
        userid = input("请输入您的身份证号码:")
        phone = input("请输入您的电话号码:")
        password = self.get_pwd()
        cardid = self.get_cardid()
        # int(str(100000 + random.random() * 100000).split('.')[0])
        money = 100
        card = Card(cardid, password, money)
        user = Person(name, userid, phone, card)
        self.user_dict[cardid] = user
        self.userid_dict[userid] = cardid
        print("恭喜{who}开卡成功,您的卡号是:{cardid},卡内余额为{money}元".format(who=name, cardid=cardid, money=money))
        print(self.user_dict)
        print(self.userid_dict)

    def get_pwd(self):
        while True:
            pwd = input("请设置您的密码:")
            pwd2 = input("请再次输入您的密码")
            if (pwd == pwd2):
                return pwd
            else:
                print("两次密码输入不一致")

    def get_cardid(self):
        # 随机 + 不重复
        while True:
            cardid = str(random.randrange(100000, 1000000))
            if cardid not in self.user_dict:
                return cardid

    def query(self):
        # 查询卡信息
        card = self.get_card_info()
        # print(card.money,"----------------------")
        if not card:
            print("该卡不存在")
        else:
            if card.islock:
                print("该卡已被冻结")
            else:
                if self.check_pwd(card):
                    print("您卡内的余额是{}元".format(card.money))

    def get_card_info(self):
        cardid = input("请输入您要查询的卡号:")
        if cardid not in self.user_dict:
            return False
        else:
            user = self.user_dict[cardid]
            return user.card

    def check_pwd(self, card):
        count = 3
        while count:
            pwd = input("请输入密码:")
            if pwd == card.password:
                return True
            else:
                count -= 1
                print("密码错误!还剩%d次机会" % (count))
                if (count == 0):
                    card.islock = True
                    print("密码错误,卡已被冻结")

    def save_money(self):
        card = self.get_card_info()
        if not card:
            print("该卡不存在")
        else:
            if card.islock:
                print("该卡已被冻结")
            else:
                user = self.user_dict[card.cardid]
                print("此卡用户名为:{}".format(user.name))
                key_sure = input("确认存款请按 1, 按任意键返回上一层")
                if key_sure == "1":
                    num = input("请输入您要存款的金额")
                    if num.isdecimal():
                        card.money += int(num)
                        print("存款成功,账户余额为:%d" % (card.money))
                    else:
                        print("输入的金额格式有误")

    def lock(self):
            card = self.get_card_info()
            if not card:
                print("该卡不存在")
            else:
                if card.islock:
                    print("该卡已被冻结,无需重复操作")
                else:
                    choice = input("1.使用密码冻结 2.使用身份证冻结")
                    if choice == "1":
                        #pwd = input("请输入该卡的密码:")
                        # if pwd == card.password:
                        if self.check_pwd(card):
                            card.islock = True
                            print("冻结成功")
                        else:
                            print("密码错误,冻结失败")
                    elif choice == "2":
                        userid = input("请输入本人身份证号码:")
                        if userid == self.user_dict[card.cardid].userid:
                            card.islock = True
                            print("冻结成功")
                        else:
                            print("身份证号码错误,冻结失败")
                    else:
                        print("选择有误")

    def unlock(self):
            card = self.get_card_info()
            if not card:
                print("该卡不存在")
            else:
                if card.islock:
                    choice = input("1.使用密码解冻 2.使用身份证解冻")
                    if choice == "1":
                        # pwd = input("请输入该卡的密码:")
                        # if pwd == card.password:
                        if self.check_pwd(card):
                            card.islock = False
                            print("解冻成功")
                        else:
                            print("密码错误,解冻失败")
                    elif choice == "2":
                        userid = input("请输入本人身份证号码:")
                        if userid == self.user_dict[card.cardid].userid:
                            card.islock = False
                            print("解冻成功")
                        else:
                            print("身份证号码错误,解冻失败")
                    else:
                        print("选择有误")
                else:
                    print("该卡未被冻结,无需解冻")

    def get_money(self):
        card = self.get_card_info()
        if not card:
            print("该卡不存在")
        else:
            if card.islock:
                print("该卡已被冻结,无法进行操作")
            else:
                if self.check_pwd(card):
                    num = input("请输入您要取的金额数 ,请输入 {} 以下数字".format(card.money))
                    if not num.isdecimal() or (int(num) > card.money or int(num) < 0):
                        print("输入有误 请重新输入(输入 0-%d)之间的数字"%(card.money))
                    else:
                        card.money -= int(num)
                        print("取款成功,您本次取走 %d 元,余额为 %d 元" % (int(num), card.money))
                else:
                    print("取钱失败")

    def trans_money(self):
        card = self.get_card_info()
        if not card:
            print("该卡不存在")
        else:
            if card.islock:
                print("该卡已被冻结,无法进行操作")
            else:
                if self.check_pwd(card):
                    herid = input("请输入她的账号")
                    if herid in self.user_dict:
                        sure = input("您正在向{}汇款,确认请按 1 按任意键返回".format(self.user_dict[herid].name))
                        if sure == "1":
                            num = input("请输入您要转账的金额")
                            if not num.isdecimal() or (int(num) > card.money or int(num) < 0):
                                print("输入有误 请重新输入(输入 0-%d)之间的数字" % (card.money))
                            else:
                                card.money -= int(num)
                                self.user_dict[herid].card.money += int(num)
                                print("转账成功,您本次转走 %d 元,余额为 %d 元" % (int(num), card.money))
                                print("这是测试,实际不显示  收到来自{}转账{}元,{}账户余额为{}元".format(self.user_dict[card.cardid].name,num,self.user_dict[herid].name,self.user_dict[herid].card.money))
                    else:
                        print("查无此人")
                else:
                    print("转账失败")

    def change_pwd(self):
        card = self.get_card_info()
        if not card:
            print("该卡不存在")
        else:
            if card.islock:
                print("该卡已被冻结,无法进行操作")
            else:
                if self.check_pwd(card):
                    pwd = self.get_pwd()
                    card.password = pwd
                    print("密码修改成功")

    def new_card(self):
        # 新卡换旧卡,信息保留
        userid = input("请输入您的身份证:")
        if userid in self.userid_dict:
            # 身份证 --- 卡号 --- 人(用户) --- 卡
            cardid = self.userid_dict[userid]
            user = self.user_dict[cardid]
            card = user.card

            new_cardid = self.get_cardid()
            res = self.user_dict.pop(cardid)  # 返回键值对的值 person 人这个对象

            card.cardid = new_cardid
            self.user_dict[new_cardid] = res

            self.userid_dict[userid] = new_cardid
            print("新卡号是%s"%(new_cardid))

        else:
            print("查无此人")


'''
        count = 3
        while count:
            if cardid in self.user_dict:
                pwd = input("请输入您的密码:")
                if pwd == self.user_dict[cardid].card.password:
                    print("登陆成功!")
                    return self.user_dict[cardid].card
                else:
                    count -= 1
                    if count==0:
                        print("输入错误,卡已被冻结")
                        self.user_dict[cardid].card.islock = True
                        break
                    print("密码错误,还有%d次机会" % (count))
            elif self.user_dict[cardid].card.islock:
                print("该卡已被冻结")
                break
            else:
                print("未查到该账户")
                break
'''
'''
请选择要办理的业务:1
请输入您的姓名:李雷
请输入您的身份证号码:332123122341414144
请输入您的电话号码:13242721642
请设置您的密码:212222
请再次输入您的密码212222
恭喜李雷开卡成功,您的卡号是:401689,卡内余额为100元
{'142431': <atmpackage.person.Person object at 0x0000021A7FADD940>, '401689': <atmpackage.person.Person object at 0x0000021A7FC7FFD0>}
{'111': 142431, '332123122341414144': '401689'}
请选择要办理的业务:0
即将退出...
'''
